package com.api.component;

import java.util.Objects;

import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.api.common.reqres.ReqResConstant;
import com.api.common.reqres.Response;

@Component
public class ResponseComponent {

	public Response toSuccess(Object data) {
		Response response = new Response();
		response.setResultCode(ReqResConstant.SUCCESS_CODE);
		response.setResultMessage(ReqResConstant.SUCCESS);
		if (!Objects.isNull(response)) {
			response.setData(data);
		}
		return response;
	}

	public <T> Response<T> toError(String message) {
		Response<T> response = new Response<T>();
		response.setResultCode(ReqResConstant.ERROR_CODE);
		response.setResultMessage(message);
		return response;
	}

	public <T> Response<T> toError(String code, String message) {
		Response<T> response = new Response<T>();
		response.setResultCode(code);
		response.setResultMessage(message);
		return response;
	}

	public String toErrorJSON(String message) {
		Response response = new Response();
		response.setResultCode(ReqResConstant.ERROR_CODE);
		response.setResultMessage(message);
		return JSON.toJSONString(response);
	}
}
